package com.smarthome.ipc.command;

import javax.annotation.Resource;

import com.smarthome.common.ipc.Command;
import com.smarthome.common.tools.IPNetUtils;
import com.smarthome.domain.ipc.IpcCodeEnum;
import com.smarthome.domain.ipc.IpcConstants;
import com.smarthome.domain.ipc.IpcResponse;
import com.smarthome.domain.ipc.req.IpcResidenceDoorRequest;
import com.smarthome.ipc.aopu.AopuTcpConstans;
import com.smarthome.ipc.aopu.AopuTcpMain;
import com.smarthome.ipc.aopu.entity.AopuDoorOpen;

/**
 * 奥普摆闸模块-开门
 * 
 * 
 * @author KongXiangyu
 * 
 * @since 1.0
 */
@org.springframework.stereotype.Service(IpcConstants.IPC_COMMAND_RESIDENCE_DOOR_OPEN_AOPU)
public class AopuIpcResidenceDoorOpen implements Command<IpcResidenceDoorRequest, IpcResponse<String>> {
	@Resource
	private AopuTcpMain aopuTcpMain;

	@Override
	public void execute(IpcResidenceDoorRequest request, IpcResponse<String> response) {

		try {
			if (!IPNetUtils.isIpv4(request.getIp())) {
				response.setIpcCodeObj(IpcCodeEnum.PARM_NOT_VALID);
				response.setData("参数非法,ip=[" + request.getIp() + "],port=[" + request.getPort() + "].");
				return;
			}
			AopuDoorOpen opendoor = new AopuDoorOpen();
			opendoor.setCommandhex(AopuTcpConstans.COMMAND_DOOR_OPEN);
			byte doorNum = 0x01;
//			if ("1".equals(request.getDeviceId())) {
//				doorNum = 0x01;
//			} else if ("2".equals(request.getDeviceId())) {
//				doorNum = 0x02;
//			} else if ("3".equals(request.getDeviceId())) {
//				doorNum = 0x04;
//			} else if ("4".equals(request.getDeviceId())) {
//				doorNum = 0x08;
//			} else {
//				response.setIpcCodeObj(IpcCodeEnum.SYSTEM_ERROR);
//				response.setData("没有找到序号为" + request.getDeviceId() + "摆闸。");
//				return;
//			}
			byte lengthL = 0x00;
			byte lengthH = 0x00;
			byte[] data = {};
			opendoor.setDoorNum(doorNum);
			opendoor.setLengthL(lengthL);
			opendoor.setLengthH(lengthH);
			opendoor.setData(data);
			opendoor.setIp(request.getIp());
			opendoor.setPort(request.getPort());
			opendoor.setAccessType(request.getAccessType().trim());
			aopuTcpMain.sendCommand(opendoor);
			response.setIpcCodeObj(IpcCodeEnum.SYSTEM_SUCCESS);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			response.setIpcCodeObj(IpcCodeEnum.SYSTEM_ERROR);
			response.setData("产生异常[" + e.getMessage() + "].");
		}
	}

}
